<template>
	<view class="apply-agent">
		<view class="banner d-c-c d-c" v-if="top_background != ''&&isApplying!=10">
			<image :src=" top_background" mode="widthFix">
			</image>
		</view>
		<!--申请成功-->
		<template v-if="isApplying==20||isApplying==30">
			<form @reset="formReset">
				<view class="form-card">
					<view class="form-header">
						{{ (words.apply && words.apply.wordTitle) || "" }}
					</view>
					<view class="form-body">
						<view class="form-item ">
							<view class="label"><span class="required">*</span>邀请人</view>
							<view class="value ">{{ refereeName || '平台' }}</view>
						</view>
						<!-- 姓名 -->
						<view class="form-item">
							<view class="label"><span class="required">*</span>姓名</view>
							<input class="value " name="name" v-model="form.name" type="text" placeholder="请输入姓名" />
						</view>
						<!-- 手机号 -->
						<view class="form-item">
							<view class="label"><span class="required">*</span>手机号</view>
							<input class="value " name="mobile" v-model="form.mobile" type="number"
								placeholder="请输入手机号" />
						</view>
						<view class="form-agree">
							<checkbox-group @change="changeFunc">
								<checkbox class="chk" value="checkbox" :checked="is_read" />
							</checkbox-group>
							<text class="agree-text">我已阅读并了解</text>
							<text class="agree-link" style="color: #ff5649;" @click="isPopup = true">
								《{{ words.apply && words.apply.license }}》
							</text>
						</view>
					</view>


				</view>
				<view class="d-c-c mt30">
					<!-- #ifdef H5 -->
					<template v-if="isWeixin() && mpState == 1 && temlIds.length > 0">
						<wx-open-subscribe :template="temlIds" id="subscribe-btn" @success="subscribeSuccess"
							@error="subscribeFail">
							<div v-is="'script'" type="text/wxtag-template" slot="style"></div>
							<div v-is="'script'" type="text/wxtag-template">
								<div class="subscribe-btn" :style="{
				  width: btnAtrrpx.width + 'px',
				  height: btnAtrrpx.height + 'px',
				  lineHeight: btnAtrrpx.height + 'px',
				  paddingLeft: btnAtrrpx.paddingLeft + 'px',
				  paddingRigth: btnAtrrpx.paddingLeft + 'px',
				  fontSize: btnAtrrpx.fontSize + 'px',
				  background: '#f6220c',
				  border: '1px solid #f6220c',
				  background: 'linear-gradient(0deg, #FFDCA2 0%, #FDC57A 100%)',
				  borderRadius: '20px',
				  color: '#fff',
				  textAlign: 'center',
				}">
									{{ words.apply && words.apply.submit }}
								</div>
							</div>
						</wx-open-subscribe>
					</template>
					<template v-else>
						<button class="btn-sub" @click="formSubmit">
							{{ words.apply && words.apply.submit }}
						</button>
					</template>
					<!-- #endif -->
					<!-- #ifndef H5 -->
					<button type="primary" class="btn-sub flex-1" @click="formSubmit">
						{{ (words.apply && words.apply.submit) || "" }}
					</button>
					<!-- #endif -->
				</view>

			</form>
		</template>
		<!--分销商审核中-->
		<template v-if="isApplying==10">
			<view class="d-c-c mt50">
				<image lazy-load style="width: 532rpx;height: 278rpx;margin: 0 auto;" src="/static/agen_applay.png"
					mode="">
				</image>
			</view>
			<view class="p-30-0 d-c-c gray9 f26">
				{{ words&& words.apply.waitAudit || '' }}
			</view>
			<view class="p30 mt30 d-c-c">
				<button type="primary" class="btn-sub"
					@click="gotoShop">{{ words&&words.apply.gotoMall || '' }}</button>
			</view>
		</template>


		<!--协议-->
		<Popup :show="isPopup" msg="申请协议">
			<scroll-view class="agreement-content f25 lh150" scroll-y @scroll="handleScroll">
				<view v-html="agreement"></view>
			</scroll-view>
			<view class="ww100 pt20 d-c-c">
				<!-- <button
          class="btn-red"
          :disabled="!isScrolledToBottom"
          @click="closePopup"
        >
          我已阅读
        </button> -->
				<button class="btn-red" @click="closePopup">我已阅读</button>
			</view>
		</Popup>
	</view>
</template>

<script>
	import Popup from "@/components/uni-popup.vue";
	export default {
		components: {
			Popup,
		},
		data() {
			return {
				/*弹窗是否打开*/
				isPopup: false,
				/*是否阅读了规则*/
				is_read: false,
				agreement: "",
				isApplying: 20,
				refereeName: "",
				words: {},
				form: {},
				isAgent: "",
				/*顶部背景*/
				top_background: "",
				/*小程序订阅消息*/
				url: "",
				temlIds: [],
				btnAtrrpx: {},
				isScrolledToBottom: false,
				mpState: null,
			};
		},

		mounted() {
			let mpState = uni.getStorageSync('mpState');
			this.mpState = mpState;
			// 公众号分享
			if (this.getPlatform() == "mp" || this.isWeixin()) {
				this.url = window.location.href;
			}
			// #ifdef H5
			if (this.isWeixin()) {
				this.getBtnInfo();
				this.getTemplateId();
			}
			// #endif
			/*数据*/
			this.getData();
		},
		methods: {
			handleScroll(e) {
				const {
					scrollTop,
					scrollHeight
				} = e.detail;
				// 判断是否滚动到底部
				if (scrollTop + 420 >= scrollHeight) {
					this.isScrolledToBottom = true;
				} else {
					this.isScrolledToBottom = false;
				}
			},
			closePopup() {
				this.isPopup = false;
				this.is_read = true; // 设置已阅读
			},
			subscribeSuccess() {
				this.formSubmit();
				console.log("调用成功");
			},
			subscribeFail() {
				this.formSubmit();
				console.log("调用失败");
			},
			getBtnInfo() {
				let self = this;
				let btnAtrrpx = {
					width: 540,
					height: 84,
					paddingLeft: 28,
					fontSize: 28,
				};
				uni.getSystemInfo({
					success: function(res) {
						let scale = res.screenWidth / 750;
						let newObj = {};
						for (let key in btnAtrrpx) {
							newObj[key] = btnAtrrpx[key] * scale;
						}
						self.btnAtrrpx = newObj;
					},
					fail() {
						self.btnAtrrpx = btnAtrrpx;
					},
				});
			},
			getTemplateId() {
				let self = this;
				self._postBody(
					"index/getSignPackage", {
						url: self.url,
						paySource: self.getPlatform(),
					},
					function(res) {
						// self.temlIds = res.data.templateArr;
						self.mpMessage(res.data.signPackage);
					}
				);
			},
			/*获取数据*/
			getData() {
				let self = this;
				uni.showLoading({
					title: "加载中",
				});
				self._get(
					"user/agent/apply", {
						platform: self.getPlatform(),
						refereeId: uni.getStorageSync("refereeId"),
					},
					function(res) {
						uni.hideLoading();
						self.top_background = res.data.apply;
						self.isApplying = res.data.isApplying;
						self.refereeName =
							res.data.refereeName != null ? res.data.refereeName : "平台";
						self.isAgent = res.data.isAgent;
						self.words = res.data.words;
						self.temlIds = res.data.templateArr;
						self.agreement = res.data.license.license;
						/*设置标题*/
						uni.setNavigationBarTitle({
							title: res.data.words.apply.wordTitle,
						});
						if (self.isAgent) {
							self.gotoPage('/pages/agent/index/index');
						}
					}
				);
			},

			/*申请*/
			formSubmit: function(e) {
				let formdata = this.form;
				formdata.refereeId = uni.getStorageSync("refereeId");
				let self = this;
				if (formdata.name == "") {
					uni.showToast({
						title: "请输入姓名！",
						icon: "none",
					});
					return;
				}
				if (formdata.mobile == "") {
					uni.showToast({
						title: "请输入手机号！",
						icon: "none",
					});
					return;
				}
				//var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
				if (formdata.mobile.length != 11) {
					uni.showToast({
						title: "手机有误,请重填！",
						icon: "none",
					});
					return;
				}
				if (!self.is_read) {
					uni.showToast({
						title: "请同意协议！",
						icon: "none",
					});
					return;
				}

				let callback = function() {
					uni.showLoading({
						title: "正在提交",
						mask: true,
					});
					let param = {};
					param = formdata;
					param.refereeId = formdata.refereeId;
					self._postBody("plus/agent/apply/submit", param, function(res) {
						uni.hideLoading();
						if (res.data.become == 10) { //后台审核
							uni.showToast({
								title: "申请成功",
							});
							self.getData();
						} else { //无需审核
							uni.redirectTo({
								url: "/pages/agent/index/index"
							})
						}

					});
				};
				self.subMessage(self.temlIds, callback);
			},

			/*去商城看看*/
			gotoShop() {
				uni.switchTab({
					url: "/pages/index/index",
				});
			},

			/*同意协议*/
			changeFunc(e) {
				if (e.detail.value.length > 0) {
					this.is_read = true;
				} else {
					this.is_read = false;
				}
			},
		},
	};
</script>

<style lang="scss">
	.apply-agent .banner {
		background-repeat: no-repeat;
		background-size: 100%;
	}

	.apply-agent .banner image {
		width: 750rpx;
		height: 382rpx;
	}

	.form-wrap {
		background: #FFFFFF;
		// box-shadow: 0 0 8rpx 0 rgba(0, 0, 0, .2);
	}



	.agreement-content {
		max-height: 60vh;
		overflow-y: auto;
	}

	.apply-agent .btn-sub {
		width: 600rpx;
		height: 88rpx;
		line-height: 88rpx;
		border-radius: 44rpx;
		background: linear-gradient(0deg, #FFDCA2 0%, #FDC57A 100%);
		color: #5A3713;
		font-size: 32rpx;
		border: none;
	}

	.form-card {
		position: relative;
		margin: -50rpx 20rpx 0;
		/* 把卡片上提覆盖 banner */
		padding: 32rpx 20rpx 54rpx;
		background: #fff;
		border-radius: 25rpx;

		.form-header {
			font-size: 32rpx;
			color: #333;
			text-align: center;
			margin-bottom: 40rpx;
			font-weight: 400;
		}

		.form-body {
			.form-item {
				display: flex;
				flex-direction: column;
				/* 纵向排列 */
				align-items: flex-start;
				/* 左对齐 */
				padding-bottom: 30rpx;


				.label {
					font-size: 26rpx;
					color: #333;
					margin-bottom: 20rpx;

					/* 字段名与输入框间距 */
					.required {
						color: #FB5032;
						margin-right: 6rpx;
					}
				}

				.value {
					// border: 1rpx solid #ebebeb;
					width: 100%;
					font-size: 26rpx;
					padding: 0 24rpx;
					background: #F6F6F6;
					border-radius: 15rpx;
					box-sizing: border-box;
					color: #333;
					height: 88rpx;
					line-height: 88rpx;
				}


			}

			.form-agree {
				display: flex;
				align-items: center;
				margin: 0;
				color: #666;

				.chk {
					transform: scale(0.7);
					margin-right: 10rpx;
					border-radius: 200rpx;
				}

				.agree-text {
					font-size: 24rpx;
					color: #666;
					line-height: 30rpx;
				}

				.agree-link {
					font-size: 24rpx;
					color: #ff5649;
					margin-left: 6rpx;
				}
			}

			.form-submit {
				text-align: center;
				margin-top: 30rpx;

				.btn-submit {
					width: 100%;
					height: 88rpx;
					line-height: 88rpx;
					border-radius: 44rpx;
					background-color: #ff5649;
					color: #fff;
					font-size: 32rpx;
				}
			}
		}
	}
</style>